package pseudoAssemblerCompiler.twopassassembler.view.secondpass;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pseudoAssemblerCompiler.twopassassembler.model.symbolnametable.SymbolicNamesContentProvider;
import pseudoAssemblerCompiler.twopassassembler.model.symbolnametable.SymbolicNamesLabelProvider;
import pseudoAssemblerCompiler.twopassassembler.view.common.IView;
import pseudoAssemblerCompiler.twopassassembler.view.util.TableViewerContainer;

/**
 * Таблица символических имён
 *
 * @author Чекашов Р. И.
 */
public class SymbolicNamesTable implements IView, ISymbolicNamesTableView {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(SymbolicNamesTable.class);

    private static final String TABLE_NAME = "Таблица символических имён";

    private Composite _composite;

    private TableViewerContainer container;

    private TableViewer _symbolicNamesTableViewer;

    /**
     * Символьное имя
     */
    private TableColumn _symbolicNameColumn;

    /**
     * Адрес
     */
    private TableColumn _addressColumn;

    /**
     * Метка
     */
    private Label _label;

    public SymbolicNamesTable(Composite aParent) {
        _composite = new Composite(aParent, SWT.NONE);

        _label = new Label(_composite, SWT.CENTER);
        _label.setText(TABLE_NAME);

        container = new TableViewerContainer(_composite);
        _symbolicNamesTableViewer = container.get_tableViewer();
        _symbolicNamesTableViewer.getTable().setHeaderVisible(true);
        _symbolicNamesTableViewer.getTable().setLinesVisible(true);

        TableViewerColumn symbolicNameTVC1 = container.createColumn("Символьное имя");
        TableViewerColumn addressTVC2 = container.createColumn("Адрес");

        _symbolicNameColumn = symbolicNameTVC1.getColumn();
        _addressColumn = addressTVC2.getColumn();

        _symbolicNamesTableViewer.setContentProvider(new SymbolicNamesContentProvider());
        _symbolicNamesTableViewer.setLabelProvider(new SymbolicNamesLabelProvider());

        container.listeners();
    }

    /**
     * Располагаем элементы в определённом порядке
     */
    private void layouts() {
        FormData data;

        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.top = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0);
        _composite.setLayout(new FormLayout());
        _composite.setLayoutData(data);

        //Label - метка
        data = new FormData();
        data.top = new FormAttachment(0, 0); // Attach to top
        data.right = new FormAttachment(100, 0);
        data.left = new FormAttachment(0, 0); // Attach halfway across
        data.height = 20;
        _label.setLayoutData(data);

        //Table - таблица
        data = new FormData();
        data.top = new FormAttachment(_label, 0); // Attach to top
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0); // Attach to bottom
        data.left = new FormAttachment(0, 0); // Attach halfway across
        container.getComposite().setLayoutData(data);
    }


    private void listeners() {

    }

    @Override
    public void setLayoutData(Object layoutData) {
        _composite.setLayoutData(layoutData);
    }

    @Override
    public void show() {
        layouts();
    }

    @Override
    public Composite getComposite() {
        return _composite;
    }

    @Override
    public void setController(Object controller) {
        listeners();
    }

    @Override
    public void setInput(Object input) {
        _symbolicNamesTableViewer.setInput(input);
    }
}
